//
//  LoginViewController.m
//  Lottery
//
//  Created by 桑赐相 on 2018/7/14.
//  Copyright © 2018年 桑赐相. All rights reserved.
//

#import "LoginViewController.h"
#import "JPushManager.h"
@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *accountTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UIButton *findPasswordButton;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"登录";
//    self.accountTextField.text = @"13760423729";
//    self.passwordTextField.text = @"sang8277536";
    [self.loginButton setLayerWithRadius:5 color:ClearColor width:0];
    [self.loginButton setBackgroundImage:[UIImage createImageWithColor:[UIColor lightGrayColor]] forState:UIControlStateDisabled];
    [self.loginButton setBackgroundImage:[UIImage createImageWithColor:BaseThemeColor] forState:UIControlStateNormal];
    RAC(self.loginButton,enabled) = [RACSignal combineLatest:@[self.accountTextField.rac_textSignal,self.passwordTextField.rac_textSignal] reduce:^id _Nonnull(NSString * username, NSString * password){
        return @(username.length > 0 && password.length > 0);
    }];
}

- (IBAction)loginAction:(UIButton *)sender {
    [self.view endEditing:YES];
    NSString *account = self.accountTextField.text;
    NSString *password = self.passwordTextField.text;
    
    if (account.length <= 0) {
        [iToast showCenter:@"登录账号不能为空!"];
        return;
    }
    if (password.length <= 0) {
        [iToast showCenter:@"登录密码不能为空!"];
        return;
    }
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"username"] = account;
    dict[@"password"] = password;
    
    MBProgressHUD *hud = [MBProgressHUD showMessage:@"登录中..."];
    [HTTPTools POST:loginUrl parameters:dict callback:^(id result, NSString *msg,NSError *error) {
        [hud hideAnimated:YES];
        if (!error) {
            if (!msg) {
                NSLog(@"%@",result);
                XLAuthModel *auth = [XLAuthModel mj_objectWithKeyValues:result];
                [XLAuthModel saveAuth:auth];
                
                [self token:auth.userID];
                [self getUserInfo:auth.userID];
            }else{
                [MBProgressHUD showError:msg];
            }
        }
    }];
}

-(void)token:(NSString *)userId{
    
    
    UIDevice *device = [UIDevice currentDevice];
    
    NSString *url = [NSString stringWithFormat:@"/api/user/%@/token",userId];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"channel"] = @"0";
    dict[@"device_id"] = device.identifierForVendor.UUIDString;
    dict[@"device_token"] = @"";
    dict[@"device_type"] = @"IOS";
    dict[@"ios_resource"] = @"2";
    dict[@"version_code"] = @"19";
    
    [HTTPTools POST:url parameters:dict callback:^(id result, NSString *msg, NSError *error) {
        if (!error) {
            if (!msg) {
                
            }
        }
    }];
}


-(void)getUserInfo:(NSString *)userId{
    LocationManager *location = [LocationManager manager];
    UIDevice *device = [UIDevice currentDevice];
    NSString *url = [NSString stringWithFormat:@"/api/user/%@",userId];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"city_id"] = @"0";
    dict[@"device_id"] = device.identifierForVendor.UUIDString;
    dict[@"latitude"] = @(location.coordinate2D.latitude);
    dict[@"longitude"] = @(location.coordinate2D.longitude);
    MBProgressHUD *hud = [MBProgressHUD showMessage:@"获取用户信息..."];
    [HTTPTools GET:url parameters:dict callback:^(id result, NSString *msg, NSError *error) {
        [hud hideAnimated:YES];
        if (!error) {
            if (!msg) {
                XLUserModel *user = [XLUserModel mj_objectWithKeyValues:result];
                [XLUserModel saveUserInfo:user];
                [self popViewController];
            }
        }
    }];
    
    
    
}


- (IBAction)registerAction:(UIButton *)sender {
    [self.view endEditing:YES];
    UIViewController *view = SBView(@"Login", @"RegisterViewController");
    [self pushViewController:view];
}

- (IBAction)findPasswordAction:(UIButton *)sender {
    [self.view endEditing:YES];
    UIViewController *view = SBView(@"Login", @"FindPwdViewController");
    [self pushViewController:view];
}


@end
